package org.procrastinationapp.web.pages;

import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.Page;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.IPageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.link.PageLink;
import org.procrastinationapp.web.pages.aaa.MyAuthenticatedWebSession;

public abstract class BasePage extends WebPage {
	transient protected Log log = LogFactory.getLog(getClass());

	/**
	 * Constructor
	 */
	public BasePage() {
		super();
		add(new PageLink("homeNavBar", new IPageLink() {
			public Page getPage() {
				return new HomePage();
			}

			public Class<? extends Page> getPageIdentity() {
				return HomePage.class;
			}
		}));

		add(new PageLink("usersNavBar", new IPageLink() {
			public Page getPage() {
				return new UserList();
			}

			public Class<? extends Page> getPageIdentity() {
				return UserList.class;
			}
		}));

		final MyAuthenticatedWebSession session = (MyAuthenticatedWebSession) getSession();

		WebMarkupContainer sessionInfo = new WebMarkupContainer("sessionInfo") {
			@Override
			public boolean isVisible() {
				return session.isSignedIn();
			}
		};
		sessionInfo.add(new Link("signOutLink") {
			public void onClick() {
				session.invalidate();
				setResponsePage(HomePage.class);
			}
		});
		sessionInfo.add(new Label("loginName", session.getUserName()));
		add(sessionInfo);
	}

	/*
	 * TODO This pattern would be get from the locale
	 */
	public static final String DATE_PATTERN = "dd/MM/yyyy";

	public String formatDate(Date date) {
		return DateFormatUtils.format(date, DATE_PATTERN);
	}
}
